<?php if(!defined('ARCH_ROOT_PATH')) exit;
/**
 *	Architect Framework
 *
 *	Architect Framework is a light weight object oriented web applications framework written for PHP 5.1.4 and later that provide the tools to develop and deploy a website or application fast and with as little code as possible.
 *
 *	@author Robin Grass, <hello@coffinboy.se>
 *	@link http://architect.kodlabbet.net
 *	@license http://www.opensource.org/licenses/gpl-3.0.html GNU General Public License (v. 3)
 */

/**
 *	Architect_StreamAbstract
 *
 *	A custom stream wrapper roughly based on the template stream wrapper classes written by Fredrik Holmtröm. All of these methods are called "magically" since it uses StreamWrapper. For more information see - http://se2.php.net/manual/en/class.streamwrapper.php
 *
 *	@package Abstracts
 *	@version 1.0
 *	@author Robin Grass, <hello@coffinboy.se>
 */
class Architect_StreamAbstract {

	/**
	 *	@var int $pos
	 */
	protected $pos = 0;
	
	/**
	 *	@var string $data
	 */
	protected $data;
	
	/**
	 *	@var mixed $pos
	 */
	protected $stat;
	
	/**
	 *	stream_open
	 *
	 *	@param string $path
	 *	@param string $mode
	 *	@param string $options
	 *	@param string $opened_path
	 *
	 *	@return bool
	 */
	abstract public function stream_open($path, $mode, $options, $opened_path);
	
	/**
	 *	stream_seek
	 *
	 *	@param string $offset
	 *	@param string $mode
	 *
	 *	@return bool true
	 */
	public function stream_seek($offset, $mode) {
		switch($mode) {
			case SEEK_SET :
				if($offset < strlen($this->data) && $offset >= 0)
					$this->pos = $offset;
				else
					return false;
				return true;
			break;
			
			case SEEK_CUR :
				if($offset >= 0)
					$this->pos += $offset;
				else
					return false;
				return true;
			break;
			
			case SEEK_END :
				if(strlen($this->data) + $offset >= 0)
					$this->pos = strlen($this->data) + $offset;
				else
					return false;
				return true;
			break;
			
			default : 
				return false;
		}
	}
	
	/**
	 *	stream_read
	 *
	 *	@param int $count
	 *
	 *	@return string $return
	 */
	public function stream_read($count) {
		$return = substr($this->data, $this->pos, $count);
		$this->pos += strlen($return);
	return $return;
	}
	
	/**
	 *	stream_tell
	 *
	 *	@return int $pos
	 */
	public function stream_tell() {
		return $this->pos;
	}
	
	/**
	 *	stream_stat
	 *
	 *	@return mixed $stat
	 */
	public function stream_stat() {
		return $this->stat;
	}
	
	/**
	 *	stream_eof
	 *
	 *	@return bool
	 */
	public function stream_eof() {
		return $this->pos >= strlen($this->data);
	}

}
?>